<template>
  <el-dialog v-model="visible" :title="!dataForm.gnssCenterId ? '新增' : '修改'" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" :inline="true" @keyup.enter="submitHandle()">
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否加密" prop="enrypt">
            <el-radio-group v-model="dataForm.enrypt">
              <el-radio :value="true">是</el-radio>
              <el-radio :value="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="平台ID" prop="gnssCenterId">
            <el-input v-model="dataForm.gnssCenterId" placeholder="平台ID" :disabled="judgement"></el-input>
          </el-form-item>
          <el-form-item label="协议号" prop="protocol">
            <el-input v-model="dataForm.protocol" placeholder="协议号"></el-input>
          </el-form-item>
          <el-form-item label="IP地址" prop="platformIp">
            <el-input v-model="dataForm.platformIp" placeholder="上级平台IP地址"></el-input>
          </el-form-item>
          <el-form-item label="平台端口" prop="platformPort">
            <el-input v-model="dataForm.platformPort" placeholder="上级平台端口"></el-input>
          </el-form-item>
          <el-form-item label="用户名" prop="userId">
            <el-input v-model="dataForm.userId" placeholder="用户名"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="dataForm.password" placeholder="密码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户类型" prop="userType">
            <el-radio-group v-model="dataForm.userType">
              <el-radio :value="'UP_USER'">上级平台</el-radio>
              <el-radio :value="'DOWN_USER'">下级平台</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="子链路连接" prop="connSubLink">
            <el-radio-group v-model="dataForm.connSubLink">
              <el-radio :value="true">是</el-radio>
              <el-radio :value="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
          </el-form-item>
          <el-form-item label="A1" prop="miyaoA">
            <el-input v-model="dataForm.miyaoA" placeholder="A1"></el-input>
          </el-form-item>
          <el-form-item label="M1" prop="miyaoM">
            <el-input v-model="dataForm.miyaoM" placeholder="M1"></el-input>
          </el-form-item>
          <el-form-item label="秘钥" prop="encryptKey">
            <el-input v-model="dataForm.encryptKey" placeholder="秘钥"></el-input>
          </el-form-item>
          <el-form-item label="C1" prop="miyaoC">
            <el-input v-model="dataForm.miyaoC" placeholder="C1"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useJt809userApi, useJt809userSubmitApi } from '@/api/gps/jt809_user'

const emit = defineEmits(['refreshDataList'])

// const disabled = ref(false)
const visible = ref(false)
const dataFormRef = ref()
const judgement = ref(false)
const dataForm = reactive({
  gnssCenterId: '',
  protocol: '',
  userType: 'UP_USER',
  platformIp: '',
  platformPort: '',
  userId: '',
  password: '',
  enrypt: true,
  encryptKey: '',
  miyaoM: '',
  miyaoA: '',
  miyaoC: '',
  connSubLink: '',
  remark: ''
})

const init = (id?: number) => {
  visible.value = true
  dataForm.gnssCenterId = ''
  judgement.value = false
  // disabled.value = true
  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields()
  }

  if (id) {
    // disabled.value = true
    judgement.value = true
    getJt809User(id)
  }
}

const getJt809User = (id: number) => {
  useJt809userApi(id).then((res) => {
    Object.assign(dataForm, res.data)
  })
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false
    }

    useJt809userSubmitApi(dataForm, judgement.value).then(() => {
      ElMessage.success({
        message: '操作成功',
        duration: 500,
        onClose: () => {
          visible.value = false
          emit('refreshDataList')
        }
      })
    })
  })
}

defineExpose({
  init
})
</script>
